package com.bfsuol.yihang.documents.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bfsuol.yihang.documents.api.QueryDocumentService;
import com.bfsuol.yihang.documents.domain.DocumentService;
import com.bfsuol.yihang.documents.dto.DocumentDTO;
import com.bfsuol.yihang.documents.entity.DocumentEntity;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class QueryDocumentServiceImpl implements QueryDocumentService {

    @Autowired
    private DocumentService documentService;

    @Override
    public Page<DocumentDTO> page(Page page1, DocumentDTO documentParam) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if(!StringUtils.isEmpty(documentParam.getName())){
            queryWrapper.like("name", documentParam.getName());
        }
        if(!StringUtils.isEmpty(documentParam.getVehicleNo())){
            queryWrapper.like("vehicle_no", documentParam.getVehicleNo());
        }
        if(documentParam.getVehicleId()!=null){
            queryWrapper.eq("vehicle_id", documentParam.getVehicleId());
        }
        Page<DocumentEntity> pages = documentService.getBaseMapper().selectPage(page1,queryWrapper);
        List<DocumentDTO> list = Lists.newArrayList();
        pages.getRecords().forEach(documentEntity -> {
            DocumentDTO documentDTO = new DocumentDTO();
            BeanUtils.copyProperties(documentEntity, documentDTO);
            list.add(documentDTO);
        });
        Page<DocumentDTO> pages1 = new Page<>();
        BeanUtils.copyProperties(pages,pages1);
        pages1.setRecords(list);
        return page1;
    }

    @Override
    public DocumentDTO getById(Long id) {
        DocumentDTO documentDTO = new DocumentDTO();
        BeanUtils.copyProperties(documentService.getById(id), documentDTO);
        return documentDTO;
    }

    @Override
    public DocumentDTO getOneByVehicleNo(String vehicleNo) {
        DocumentEntity documentEntity = documentService.getOne(new QueryWrapper<DocumentEntity>().eq("vehicle_no", vehicleNo));
        DocumentDTO documentDTO = new DocumentDTO();
        if(documentEntity==null){
            return null;
        }
        BeanUtils.copyProperties(documentEntity, documentDTO);
        return documentDTO;
    }
}
